429C - Guess the Tree - CodeForces Solution


bitmasks constructive algorithms dp greedy trees *2300

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>

#define endl '\n'

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 30;
const int mod = 1e9 + 7;

int n;
int a[N], child[N];

void dfs(int u) {
    if (a[u] == 2) {
        cout << "NO" << endl;
        exit(0);
    }

    if (a[u] == 1 || u == 0) {
        cout << "YES" << endl;
        exit(0);
    }

    for (int i = n; i > u; i--) {
        if (child[i] + a[u] < a[i] && a[u] != a[i] - 1) {
            child[i] += a[u];
            dfs(u - 1);
            child[i] -= a[u];
        }
    }
}


void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);

    if (a[n] != n || n == 2) {
        cout << "NO" << endl;
        return;
    }

    dfs(n - 1);
    cout << "NO" << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T;
    T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

630P - Area of a Star
1030C - Vasya and Golden Ticket
1529D - Kavi on Pairing Duty
1743A - Password
1743B - Permutation Value
1743C - Save the Magazines
1743D - Problem with Random Tests
1070K - Video Posts
767C - Garland
1201B - Zero Array
1584C - Two Arrays
1131C - Birthday
1285B - Just Eat It
1743F - Intersection and Union
771A - Bear and Friendship Condition
1208E - Let Them Slide
656A - Da Vinci Powers
1025A - Doggo Recoloring
257A - Sockets
231C - To Add or Not to Add
1454E - Number of Simple Paths
931B - World Cup
934B - A Prosperous Lot
999B - Reversing Encryption
1238D - AB-string
810B - Summer sell-off
84A - Toy Army
185A - Plant
1749A - Cowardly Rooks
1749C - Number Game